package com.headStorm.socket.mina;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.headStorm.mavlinkDemo.Convert;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MessageHandler extends IoHandlerAdapter {

    private static Logger logger = LoggerFactory.getLogger(MessageHandler.class);

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        IoBuffer recv = (IoBuffer) message;
        // 将IBuffer转换为字节数组
        byte[] data = new byte[recv.remaining()];
        recv.get(data, 0, data.length);

        String join = ArrayUtil.join(data, ",");
        System.out.println(join);
        //放入队列中
        String msg = "";
        // 用16进制的格式打印出来显示
        msg = Convert.bytesToHexString(data, true);
        System.out.println("msg: " + msg);
        System.out.println(new String(data));
        System.out.println("终止");
    }


    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        // 空闲时，关闭session
        if (status == IdleStatus.READER_IDLE) {
            System.out.println("session进入空闲，准备关闭session");
            session.close(true);
        }
    }

    private String timeStamp2Date(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static double getAzimuth(Point pA, Point pB) {
        double a = (90 - pB.getLat()) * Math.PI / 180;
        double b = (90 - pA.getLat()) * Math.PI / 180;
        double AOC_BOC = (pB.getLon() - pA.getLon()) * Math.PI / 180;
        double cosc = Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(AOC_BOC);
        double sinc = Math.sqrt(1 - cosc * cosc);
        double sinA = Math.sin(a) * Math.sin(AOC_BOC) / sinc;
        double A = Math.asin(sinA) * 180 / Math.PI;
        double res = 0;
        if (pB.getLon() > pA.getLon() && pB.getLat() > pA.getLat()) res = A;
        else if (pB.getLon() > pA.getLon() && pB.getLat() < pA.getLat()) res = 180 - A;
        else if (pB.getLon() < pA.getLon() && pB.getLat() < pA.getLat()) res = 180 - A;
        else if (pB.getLon() < pA.getLon() && pB.getLat() > pA.getLat()) res = 360 + A;
        else if (pB.getLon() > pA.getLon() && pB.getLat() == pA.getLat()) res = 90;
        else if (pB.getLon() < pA.getLon() && pB.getLat() == pA.getLat()) res = 270;
        else if (pB.getLon() == pA.getLon() && pB.getLat() > pA.getLat()) res = 0;
        else if (pB.getLon() == pA.getLon() && pB.getLat() < pA.getLat()) res = 180;
        return res;
    }

    class Point {
        private Double lon;
        private Double lat;

        public Double getLon() {
            return lon;
        }

        public void setLon(Double lon) {
            this.lon = lon;
        }

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }
    }

}
